/**
 * @author 0283000106
 * @date 2020年2月23日-下午2:32:37
 */
package com.zte.claa.inficombo.csa.app.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.zte.claa.inficombo.csa.app.comm.csif.CSIFCmdBuilder;
import com.zte.claa.inficombo.csa.app.controller.ret.AicPhotoQryAck;
import com.zte.claa.inficombo.csa.app.dao.mysql.impl.CameraPhotoDataMapper;
import com.zte.claa.inficombo.csa.app.service.photo.bean.CameraPhotoData;
import io.swagger.annotations.ApiOperation;

/**
 * @author 0283000115
 * 
 * 该controller仅用于接收AIC的图像数据获取请求.
 *
 */

@RestController
@RequestMapping("/inficombo/apis/")
public class AicApiController extends BaseController {
	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	private static final String AIC_ACCESS_TOKEN = "Claa2017";
	// logger
	private Logger logger = LoggerFactory.getLogger(AicApiController.class);
	
	@Autowired
	private CameraPhotoDataMapper photoDataMapper;
	
	/**
	 * AIC查询CSA中保存的图像数据
	 * 
	 * 举例： 
	 * <img  src="/inficombo/apis/meters?imageid=P230u34364034d46691c79"  />
	 * 
	 * ctx+'/inficombo/apis/meters';	
	   var ajaxPara = {imageid: imageid}; 
	 */
	@ApiOperation(value = "AIC查询CSA中保存的图像数据", notes = "")
	@RequestMapping(value={"/meters"}, method = RequestMethod.GET)
	@ResponseBody
	public AicPhotoQryAck getAicPhotoData(HttpServletRequest request, HttpServletResponse response) throws Exception{
		logger.info("Accept getAicPhotoData request message");
		String tokenStr = request.getParameter("token");
		if(!AIC_ACCESS_TOKEN.equals(tokenStr)){
			AicPhotoQryAck ret1 = new AicPhotoQryAck();
			ret1.setCode("404");
			ret1.setBlob("");
			logger.error("Deal getAicPhotoData request message,token["+tokenStr+"] is error");
			return ret1;
		}
		AicPhotoQryAck ret = new AicPhotoQryAck();
		String imageid = request.getParameter("imageid");
		CameraPhotoData photodata = null;
		try {
			photodata = this.photoDataMapper.queryCameraPhotoData(imageid);			
			if (photodata == null){
				ret.setCode("404");
				ret.setBlob("");
				logger.error("Deal getAicPhotoData, photo data is null");
			}else{
				ret.setCode("202");
				ret.setBlob(CSIFCmdBuilder.base64Encode(photodata.getPhoto()));
			}
		} catch(Exception e){
			logger.error("Get aic photo data failed: "+(photodata == null? null: photodata.getPhotoid()), e);
		} finally {

		}
		return ret;
	}	
}
